﻿using System;

namespace LogNaga.Logs
{
	/// <summary>
	/// 需要文本记录的log
	/// </summary>
	/// <remarks>需要记录文本内容的</remarks>
	public abstract class TextLog : Log
	{
		/// <summary>
		/// 不允许实例化该类
		/// </summary>
		protected TextLog()
		{
		}

		#region override Log.Write

		/// <summary>
		/// 重写Log.Write方法
		/// </summary>
		/// <param name="errorEntry"><see cref="Error"/>实例</param>
		public sealed override void Write(Error errorEntry)
		{
			if (errorEntry == null)
			{
				throw new ArgumentNullException("errorEntry");
			}
			if (string.IsNullOrEmpty(errorEntry.ErrorMessage))
			{
				//如果没有要写入的内容直接return
				//不要再抛出异常
				if(errorEntry.Exception == null)
					return;

				errorEntry.ErrorMessage = GetExceptionFormatter()
					(errorEntry.Exception);
			}

			WriteToDestination(errorEntry);
		}

		#endregion

		/// <summary>
		/// 记录到数据源的具体实现
		/// </summary>
		/// <param name="errorEntry"><see cref="Error"/>实例</param>
		public abstract void WriteToDestination(Error errorEntry);

		/// <summary>
		/// 设置ExceptionFormatter
		/// </summary>
		/// <param name="exceptionFormatter"></param>
		public virtual ExceptionFormatter GetExceptionFormatter()
		{
			return ExceptionFormatterHelper.Default;
		}
	}
}
